package cn.edu.stu.syllabus.library.detail;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.like.LikeButton;
import com.like.OnLikeListener;

import javax.inject.Inject;

import butterknife.BindView;
import cn.edu.stu.syllabus.R;
import cn.edu.stu.syllabus.base.BaseActivity;
import cn.edu.stu.syllabus.repository.library.local.BookEntry;
import cn.edu.stu.syllabus.utils.ActivityUtils;
import cn.edu.stu.syllabus.utils.ExtraNameJoiner;
import cn.edu.stu.syllabus.utils.GuideHelper;

public class BookDetailActivity extends BaseActivity {

    @Inject
    BookDetailPresenter mBookDetailPresenter;

    Intent resultIntent;

    public static final int RESULT_STAR_STATE_CHANGE = 200;

    private static final String EXTRA_BOOK_ENTRY = ExtraNameJoiner.join(
            BookDetailActivity.class, BookEntry.class.getSimpleName()
    );

    public static final String EXTRA_IS_STAR = ExtraNameJoiner.join(BookDetailActivity.class
            , "isStar");

    @BindView(R.id.likeButton)
    LikeButton mLikeButton;

    public interface OnStarButtonStateChangeListener {
        boolean isStar();

        void onStateChange(boolean isStar);
    }

    public OnStarButtonStateChangeListener mOnStarButtonStateChangeListener;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_book_detail;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setTitle("书籍详情");

        BookDetailFragment bookDetailFragment = (BookDetailFragment) getSupportFragmentManager()
                .findFragmentById(R.id.contentFrame);
        if (bookDetailFragment == null) {
            bookDetailFragment = BookDetailFragment.newInstance();
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(),
                    bookDetailFragment, R.id.contentFrame);
        }

        BookEntry bookEntry = getIntent().getParcelableExtra(EXTRA_BOOK_ENTRY);

        DaggerBookDetailComponent.builder()
                .bookDetailPresenterModule(
                        new BookDetailPresenterModule(bookDetailFragment, bookEntry))
                .build().inject(this);

        mOnStarButtonStateChangeListener = bookDetailFragment;

        if (mOnStarButtonStateChangeListener != null) {
            mLikeButton.setLiked(mOnStarButtonStateChangeListener.isStar());
        }

        resultIntent = new Intent();
        mLikeButton.setOnLikeListener(new OnLikeListener() {
            @Override
            public void liked(LikeButton likeButton) {
                if (mOnStarButtonStateChangeListener != null) {
                    mOnStarButtonStateChangeListener.onStateChange(true);
                    setResultIsStar(true);
                }
            }

            @Override
            public void unLiked(LikeButton likeButton) {
                if (mOnStarButtonStateChangeListener != null) {
                    mOnStarButtonStateChangeListener.onStateChange(false);
                    setResultIsStar(false);
                }
            }
        });

        GuideHelper.startActSeqNow(this,
                GuideHelper.getToolbarItemViewTapTarget(mLikeButton,
                        "这是收藏开关按钮",
                        "点击后, 你可以将该书籍信息(加入/取出)收藏夹")
                        .id(1)
        );
    }

    public static Intent newIntent(Context context, BookEntry bookEntry) {
        Intent intent = new Intent(context, BookDetailActivity.class);
        intent.putExtra(EXTRA_BOOK_ENTRY, bookEntry);
        return intent;
    }

    private void setResultIsStar(boolean isStar) {
        resultIntent.putExtra(EXTRA_IS_STAR, isStar);
        setResult(RESULT_STAR_STATE_CHANGE, resultIntent);
    }

}
